from kivy.lang import Builder
from kivymd.uix.fitimage import FitImage
from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.card import MDCard

from Modules.db_commnads import get_favorites_genres


def online_page(obj: MDBoxLayout):

    def press_button(self):
        print(self.id)
        content.ids.online_content.clear_widgets()

    obj.clear_widgets()
    content = Builder.load_file('View/Templates/online_page.kv')
    count_fgenres: int = 0
    for item in get_favorites_genres():
        count_fgenres += 1
        card = MDCard(
            id=item['url'],
            orientation='vertical',
            md_bg_color=(0, 0, 0, 0),
            on_press=press_button
        )
        card.add_widget(
            FitImage(source=item['img'], radius=15, size_hint=(None, None))
        )
        card.add_widget(
            MDLabel(text=item['name'], halign="center")
        )
        content.ids.scroll_genres.add_widget(card)
    content.ids.scroll_genres.size_hint = (count_fgenres / 10, .9)
    content.ids.scroll_genres.pos_hint = {'center_x': .5, 'center_y': .5}
    obj.add_widget(content)
